package com.xinchao.controller;

import com.alibaba.fastjson.JSON;
import com.xinchao.constant.ResponseConstant;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.model.vo.DataReportVO;
import com.xinchao.service.DataReportService;
import com.xinchao.service.DeviceInfoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

/**
 * 数据报表Controller
 *
 * @author dxy
 * @date 2018/10/10 9:26
 */
@Controller
@RequestMapping(value = "/dataReport")
public class DataReportController {
	private static Logger logger = LoggerFactory.getLogger(DataReportController.class);
	@Autowired
	private DataReportService dataReportService;
	@Autowired
	private DeviceInfoService deviceInfoService;

	/**
	 * 跳转至总体数据
	 */
	@RequestMapping(value = "/goTotalData")
	public String goTotalData(String advertisementName, String startTime, String endTime, Model model) {
		model.addAttribute("advertisementName", advertisementName);
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		return "total_data";
	}

	/**
	 * 获取总体数据(时间格式为2018-10-10)
	 */
	@ResponseBody
	@RequestMapping(value = "/listTotalData")
	public Object listTotalData(String advertisementName, String startTime, String endTime) {
		//开始时间
		if (StringUtils.isBlank(startTime)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "开始时间不能为空");
		}
		//结束时间
		if (StringUtils.isBlank(endTime)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "结束时间不能为空");
		}
		try {
			return dataReportService.listTotalData(advertisementName, startTime, endTime);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("DataReportController listTotalData 获取总体数据失败：" + e.getMessage());
			return e.getMessage();
		} catch (ParseException e) {
			e.printStackTrace();
			logger.error("DataReportController listTotalData 获取总体数据失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}

	/**
	 * 跳转至时段分析
	 */
	@RequestMapping(value = "/goTimeFrameAnalysisData")
	public String goTimeFrameAnalysisData() {
		return "time_frame_analysis_data";
	}

	/**
	 * 获取时段分析数据
	 */
	@ResponseBody
	@RequestMapping(value = "/listTimeFrameAnalysisData")
	public Object listTimeFrameAnalysisData(String advertisementName, String startTime) {
		//开始时间
		if (StringUtils.isBlank(startTime)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "开始时间不能为空");
		}
		try {
			return dataReportService.listTimeFrameAnalysisData(advertisementName, startTime);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("DataReportController listTimeFrameAnalysisData 获取时段分析数据失败：" + e.getMessage());
			return e.getMessage();
		} catch (ParseException e) {
			e.printStackTrace();
			logger.error("DataReportController listTimeFrameAnalysisData 获取时段分析数据失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}

	/**
	 * 跳转至点位分析
	 */
	@RequestMapping(value = "/goPointLocationData")
	public String goPointLocationData(Model model) {
		List<String> deviceNumberList = deviceInfoService.listAllDeviceNumber();
		model.addAttribute("deviceNumberList", deviceNumberList);
		return "point_location_data";
	}

	/**
	 * 获取点位分析数据
	 */
	@ResponseBody
	@RequestMapping(value = "/listPointLocationData")
	public Object listPointLocationData(String advertisementName, String startTime, String endTime, String deviceNumber) {
		//开始时间
		if (StringUtils.isBlank(startTime)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "开始时间不能为空");
		}
		//结束时间
		if (StringUtils.isBlank(endTime)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "结束时间不能为空");
		}
		//机器码
		if (StringUtils.isBlank(deviceNumber)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "机器码不能为空");
		}
		try {
			return dataReportService.listDataReportPointLocationData(advertisementName, startTime, endTime, deviceNumber);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("DataReportController listDataReportPointLocationData 获取点位分析数据失败：" + e.getMessage());
			return e.getMessage();
		} catch (ParseException e) {
			e.printStackTrace();
			logger.error("DataReportController listDataReportPointLocationData 获取点位分析数据失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}
}
